package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher;

import com.google.gwt.core.shared.GWT;
import com.gwtext.client.core.Function;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.enricher.CancelEnricherResult;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.enricher.EnrichmentResult;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.enricher.actions.CreateTriplesResult;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.GWTExtUtil;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.StkosPanelType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.interfaces.StkosCallBack;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.currentdesc.StkosEnricherCurrentDescriptionPortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.currenttree.StkosEnricherCurrentTreePortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.option.StkosEnricherOptionPortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.option.StkosEnricherOptionView.StkosEnricherOptionViewButtonType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.recommended.StkosEnricherRecommendedPortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.recommended.StkosEnricherRecommendedPortlet.StkosEnricherRecButtonType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.statistical.StkosEnricherStatisticalPortlet;
import edu.stanford.bmir.protege.web.client.ui.tab.AbstractTab;

public class StkosEnricherTab extends AbstractTab {

	/** Column 1 **/
	// Row 1--- 选项
	private StkosEnricherOptionPortlet optionPortlet;

	// Row 2--- 当前本体树
	private StkosEnricherCurrentTreePortlet currentTreePortlet;

	// Row 3--- 当前本体描述
	private StkosEnricherCurrentDescriptionPortlet currentDescriptionPortlet;

	/** Column 2 **/
	// Row 1--- 统计结果
	private StkosEnricherStatisticalPortlet statisticalPortlet;

	// Row 2--- 推荐条目
	private StkosEnricherRecommendedPortlet recommendedPortlet;

	public StkosEnricherTab(Project project) {
		super(project);
	}

	@Override
	public void setup() {
		super.setup();
		setLabel("丰富工具");

		optionPortlet = (StkosEnricherOptionPortlet) getPortletByClassName(StkosEnricherOptionPortlet.class
				.getName());
		currentTreePortlet = (StkosEnricherCurrentTreePortlet) getPortletByClassName(StkosEnricherCurrentTreePortlet.class
				.getName());
		currentDescriptionPortlet = (StkosEnricherCurrentDescriptionPortlet) getPortletByClassName(StkosEnricherCurrentDescriptionPortlet.class
				.getName());

		statisticalPortlet = (StkosEnricherStatisticalPortlet) getPortletByClassName(StkosEnricherStatisticalPortlet.class
				.getName());
		recommendedPortlet = (StkosEnricherRecommendedPortlet) getPortletByClassName(StkosEnricherRecommendedPortlet.class
				.getName());

		if (optionPortlet == null || currentTreePortlet == null
				|| currentDescriptionPortlet == null
				|| statisticalPortlet == null || recommendedPortlet == null) {
			GWT.log("Some portlet can not init.");
			return;
		}

		setControllingPortlet(optionPortlet);

		registerCurrentTreePortletListener();
		addOptionViewEventHandler();
		addRecommendedEventHandler();
	}

	protected void addRecommendedEventHandler() {
		// button addAsChildOrAttr handler
		StkosCallBack addAsChildOrAttrHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				if (objects == null || objects.length == 0
						|| objects[0] == null) {
					// MessageBox.showAlert("提示", "丰富结果为空!");
					return;
				}
				CreateTriplesResult result = (CreateTriplesResult) objects[0];
				statisticalPortlet.getStatisticalView().setCreateTriplesResult(
						result);
			}
		};
		recommendedPortlet.setBtnHandler(
				StkosEnricherRecButtonType.ADDASCHILDORATTR,
				addAsChildOrAttrHandler);
	}

	protected void addOptionViewEventHandler() {
		// button autoEnricher handler
		StkosCallBack autoEnricherHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				if (objects == null || objects.length == 0
						|| objects[0] == null) {
					MessageBox.showAlert("提示", "丰富结果为空!");
					return;
				}
				EnrichmentResult result = (EnrichmentResult) objects[0];
				statisticalPortlet.getStatisticalView().setEnrichmentResult(
						result);

				currentTreePortlet.onRefresh();
				currentDescriptionPortlet.onRefresh();
			}
		};
		optionPortlet.getOptionView().setBtnHandler(
				StkosEnricherOptionViewButtonType.AUTOENRICHER,
				autoEnricherHandler);
		// button showClass handler
		StkosCallBack showClassHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				currentTreePortlet.showPanel(StkosPanelType.CLASS_TREE);
				currentDescriptionPortlet.setSelectedTreeNode(
						currentTreePortlet.getActivityPanelType(),
						currentTreePortlet.getSelectedTreeNode());
			}
		};
		optionPortlet.getOptionView().setBtnHandler(
				StkosEnricherOptionViewButtonType.SHOWCLASS, showClassHandler);
		// button showProperty handler
		StkosCallBack showPropertyHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				currentTreePortlet.showPanel(StkosPanelType.PROPERTY_TREE);
				currentDescriptionPortlet.setSelectedTreeNode(
						currentTreePortlet.getActivityPanelType(),
						currentTreePortlet.getSelectedTreeNode());
			}
		};
		optionPortlet.getOptionView().setBtnHandler(
				StkosEnricherOptionViewButtonType.SHOWPROPERTY,
				showPropertyHandler);
		// button export handler
		StkosCallBack exportHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				// MessageBox.showAlert("Export Result.");
			}
		};
		optionPortlet.getOptionView().setBtnHandler(
				StkosEnricherOptionViewButtonType.EXPORTRESULT, exportHandler);
		// button cancle click handler
		StkosCallBack cancleCB = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				// MessageBox.showAlert("cancleCB");
				CancelEnricherResult result = null;
				try {
					result = (CancelEnricherResult) objects[0];
				} catch (Exception e) {
				}

				currentTreePortlet.onRefresh();
				currentDescriptionPortlet.onRefresh();
				statisticalPortlet.getStatisticalView().setEnrichmentResult(null);
			}
		};
		optionPortlet.getOptionView().setBtnHandler(
				StkosEnricherOptionViewButtonType.CANCEL, cancleCB);
	}

	protected void registerCurrentTreePortletListener() {
		if (currentTreePortlet == null) {
			return;
		}
		currentTreePortlet.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {

					@Override
					public void execute() {
						currentDescriptionPortlet.setSelectedTreeNode(
								currentTreePortlet.getActivityPanelType(),
								currentTreePortlet.getSelectedTreeNode());
					}

				});
		currentTreePortlet.addListener(GWTExtUtil.EVENT_ONDBLCLICK,
				new Function() {

					@Override
					public void execute() {
						recommendedPortlet.mark();
						recommendedPortlet.setSelectedTreeNode(
								currentTreePortlet.getActivityPanelType(),
								optionPortlet.getOptionView().getOptions(),
								currentTreePortlet.getSelectedTreeNode());
					}

				});
	}
}
